<template>
	<view>
		<uni-forms :modelValue="Memorandum">
			<uni-forms-item label="ID" v-if="false" >
				<uni-easyinput v-model="Memorandum.bId" />
			</uni-forms-item>
			<uni-forms-item label="用户ID" v-if="false" >
				<uni-easyinput v-model="Memorandum.userId"/>
			</uni-forms-item>
			<uni-forms-item label="标题">
				<uni-easyinput  v-model="Memorandum.title" />
			</uni-forms-item>
			<uni-forms-item label="日期时间">
				<uni-datetime-picker type="datetime" return-type="timestamp" v-model="Memorandum.date"/>
			</uni-forms-item>
			<uni-forms-item label="内容">
				<uni-easyinput type="textarea" v-model="Memorandum.content" />
			</uni-forms-item>		
		</uni-forms>
		<view class="button-group">
			<button type="primary" size="mini" @click="del(Memorandum.bId)">删除</button>
			<button type="primary" size="mini" @click="submit()">保存</button>
		</view>	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id:"",
				token:"",
				Memorandum: {
					bId:"",
					userId:"",
					title:"",
					date:"",
					content:""
				}
			}
		},
		onLoad(options) {
			uni.getStorage({
				key:'token',
				success: res=>{
					// console.log(res.data);
					this.token=res.data;
					this.id=options.id;
					// console.log(options);
					uni.request({
						url:'http://8.130.170.106:9999/memorandum/selectById',
						data:{
							bId: this.id
						},
						header:{
							token: this.token // 设置 Token
						},
						success: (res) => {
							// console.log(res.data);
							this.Memorandum.bId = res.data.bid;
							this.Memorandum.userId = res.data.userId;
							this.Memorandum.title = res.data.title;
							this.Memorandum.date = res.data.date;
							this.Memorandum.content = res.data.content;
							console.log(this.Memorandum);
						}
					})
					
				}
			});
			
			
		},
		methods: {
			del(bId){
				uni.showModal({
					title: '确认删除',
					content: '确定要删除吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'http://8.130.170.106:9999/memorandum/delete',
								data: {
									bId: bId
								},
								header:{
									token:this.token
								},
								success: (res) => {
									uni.switchTab({
										url: '/pages/main/tabbar/tabbar-4/tabbar-4'
									})
								}
							})
						}
					}
				})
			},
			submit(){
				uni.request({
					url:'http://8.130.170.106:9999/memorandum/update',
					data:{
						bId: this.Memorandum.bId,
						userId: this.Memorandum.userId,
						title: this.Memorandum.title,
						content: this.Memorandum.content
					},
					header:{
						token:this.token
					},
					success: (res) => {
						uni.switchTab({
							url: '/pages/main/tabbar/tabbar-4/tabbar-4',
							success: () => {
							  console.log("跳转成功");
							},
							fail: (err) => {
							  console.log("跳转失败", err);
							}
						});
						
					}
				})
			}
		}
	}
</script>

<style>
	.title {
	  font-weight: bold;
	  font-size: 16px;
	}
	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}
	.button {
			display: flex;
			align-items: center;
			height: 35px;
			margin-left: 10px;
		}
</style>
